<?php

namespace App\Http\Controllers;

use App\Common\Constant\Params;
use App\Services\IPayService;
use App\Services\IWeinxinQyService;
use Illuminate\Http\Request;
use Log;

class CallbackController extends BaseController
{
    //
    function __invoke()
    {
        return 'callback';
    }

    /**
     * 微信企业号，系统事件接收URL
     */
    public function weixinQy(Request $request,IWeinxinQyService $weinxinQyService)
    {
        if ($request->has('echostr'))
        {
            $echoStr = $weinxinQyService->VerifyURL($request->input('msg_signature'),$request->input('timestamp'),
                $request->input('nonce'),$request->input('echostr'));
            return response($echoStr);
        }

        // 解析之后的明文
        $data = $weinxinQyService->DecryptMsg($request->input('msg_signature'),$request->input('timestamp'),
            $request->input('nonce'),$request->getContent());
    }

    /**
     * 微信支付，通知回调
     * @param Request $request
     */
    public function wxPay(Request $request,IPayService $payService)
    {
        Log::info('wxpay callback',['HTTP_RAW_POST_DATA'=>file_get_contents('php://input')]);
        $payService->setPaymentMethod(Params::PAY_METHOD_WEIXIN);
        $payService->handleNotify();
    }
}
